package com.example.qx;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.view.menu.MenuView;


import com.example.qx.database.data;
import com.example.qx.lei.leilianxr;
import com.example.qx.lei.zhangmi;

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CountDownLatch;

public class main extends AppCompatActivity {
    private Button but,butzc;
    private String mima;
    private  int zhanghao;
    private EditText zhang,mi;
    private zhangmi zhangmi= com.example.qx.lei.zhangmi.getInstance();
    private data data=new data();

    public main() throws SQLException, ClassNotFoundException, IllegalAccessException, InstantiationException {
    }

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       //data.deleteqb(getApplication());
        but=findViewById(R.id.dl);
        zhang=findViewById(R.id.zhanghao);
        mi=findViewById(R.id.mima);

        but.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final Connection[] connection = new Connection[1];
                final leilianxr[] leilianxr = new leilianxr[1];
                CountDownLatch latch=new CountDownLatch(1);
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {Class.forName("com.mysql.jdbc.Driver").newInstance();}
                        catch (IllegalAccessException | ClassNotFoundException |
                               InstantiationException e) {
                            throw new RuntimeException(e);}
                        try {
                            connection[0] = DriverManager.getConnection("jdbc:mysql://sh-cdb-9761pjk2.sql.tencentcdb.com:29081/youqi","root","asd123456");

                            String a=zhang.getText().toString();
                            zhanghao=Integer.parseInt(a);
                            System.out.println(zhanghao);
                            mima=mi.getText().toString();
                            int b=0,d;
                            String c = null;
                            try {
                                d=data.hdzhanghao(zhanghao,connection[0]);
                            } catch (SQLException  e) {
                                throw new RuntimeException(e);
                            }
                            if(d==1){
                                try {
                                    c=data.hdmima(zhanghao,connection[0]);
                                } catch (SQLException e) {
                                    throw new RuntimeException(e);
                                }


                                if(Objects.equals(c, mima))
                                    b=1;

                                if(b!=1){
                                    display();
                                }

                                else{
                                    zhangmi.setZhang(zhanghao);
                                    Intent intent =new Intent(main.this,zy.class);
                                    startActivity(intent);
                                }
                            }
                            else {
                                display1();
                            }
                        } catch (SQLException e) {throw new RuntimeException(e);}
                        latch.countDown();
                    }
                }).start();
                try{
                    latch.await();
                    connection[0].close();
                }
                catch (InterruptedException | SQLException e)
                {e.printStackTrace();}


            }});


        butzc=findViewById(R.id.zc);
        butzc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(main.this,zc.class);
                startActivity(intent);
            }
        });


             }
             private void display(){
                 AlertDialog.Builder builder=new AlertDialog.Builder(this);
                 builder.setMessage("账号或密码错误!!!");
                      AlertDialog dialog=builder.create();
                           dialog.show();
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                dialog.dismiss();
                            }
                        },1000);

                         }
    private void display1(){
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setMessage("账号不存在!!!");
        AlertDialog dialog=builder.create();
        dialog.show();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                dialog.dismiss();
            }
        },1000);

    }

         }








